<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="height: 40px; width: 100%"
  >
    <el-col>
      <!-- 名称应该变成 对应的节点中的name -->
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <!-- 两个内容 -->
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 element -->
          <el-dropdown @command="operateDepts">
            <span> 操作<i class="el-icon-arrow-down" /> </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit"
                >编辑部门</el-dropdown-item
              >
              <el-dropdown-item v-if="!isRoot" command="delete"
                >删除部门</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from "@/api/departments";
export default {
  name: "My-TreeTools",
  props: {
    treeNode: {
      type: Object,
      required: true,
    },
    // 如果是根节点 则隐藏删除和编辑
    isRoot: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    operateDepts(type) {
      if (type === "add") {
        // 新增
        this.$emit("addDepts", this.treeNode);
      } else if (type === "edit") {
        // 修改,编辑
        this.$emit("editDepts", this.treeNode);
      } else {
        //删除
        this.$confirm("是否删除该部门？")
          .then(() => {
            // 返回一个Promise resovel
            return delDepartments(this.treeNode.id);
          })
          .then(() => {
            // 确定删除
            // 子传父，更新数据
            this.$emit("delDepts");
            this.$message.success("删除成功");
          });
      }
    },
  },
};
</script>

<style scoped lang='less'></style>